package com.example.qihuibao.utils.extension

import android.content.Context
import android.view.Gravity
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast


/**
 *     author : cwwei
 *     e-mail : 494545699@qq.com
 *     time   : 2021/06/30
 *     desc   : context的扩展
 */

/**
 * 扩展toast
 */
fun Context.toast(msg: String?, duration: Int = Toast.LENGTH_SHORT){
    val toast:Toast = Toast.makeText(applicationContext, msg+"", duration)
    toast.setGravity(Gravity.CENTER, 0,0)  //居中
//    toast.view?.setBackgroundResource(R.drawable.shape_gray_round_corner)  //背景(api29兼容有问题了)
//    val layout: LinearLayout = toast.view as LinearLayout
//    val textView: TextView = layout.getChildAt(0) as TextView
//    textView.setTextColor(Color.WHITE)  //文字颜色
//    textView.setPadding(8, 0, 8, 0)
    //设置字体大小
    try {
        val linearLayout = toast.view as LinearLayout
        val messageTextView = linearLayout.getChildAt(0) as TextView
        messageTextView.textSize = 30f
    }catch (e:Exception) {
        //
    }
    toast.show()
}

/**
 * 解决小米等toast带应用名
 */
fun Context.toast2(msg: String?, duration: Int = Toast.LENGTH_SHORT){
    val toast = Toast.makeText(applicationContext, "", duration)
    toast.setGravity(Gravity.CENTER, 0,0)  //居中
//    toast.view?.setBackgroundResource(R.drawable.base_half_transparent_conner_bg)  //背景(api29兼容有问题了)
//    val layout: LinearLayout = toast.view as LinearLayout
//    val textView: TextView = layout.getChildAt(0) as TextView
//    textView.setTextColor(Color.WHITE)  //文字颜色
//    textView.setPadding(8, 0, 8, 0)
    toast.setText(msg)
    toast.show()
}

fun Context.getApplicationContext() : Context {
    return applicationContext
}
